package com.yanyu.space.sys.controller;

import com.yanyu.space.common.core.bean.web.R;
import com.yanyu.space.sys.bean.po.Dept;
import com.yanyu.space.sys.bean.vo.common.TreeSelect;
import com.yanyu.space.sys.bean.vo.dept.DeptAddVo;
import com.yanyu.space.sys.bean.vo.dept.DeptUpdateVo;
import com.yanyu.space.sys.bean.vo.dept.RoleDeptTreeselectVo;
import com.yanyu.space.sys.service.IDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author yanyu
 */
@RestController
@Slf4j
@RequestMapping("/sys/dept")
@Api(tags = "系统部门")
@Validated
public class DeptController {

    @Autowired
    private IDeptService deptService;

    @GetMapping("/list")
    @ApiOperation(value = "查询")
    public R<List<Dept>> list(String deptName, Integer enabled) {
        List<Dept> page = deptService.list(deptName, enabled);
        return R.ok(page);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public R<Dept> getById(@PathVariable("id") Long id) {
        Dept dept = deptService.getById(id);
        return R.ok(dept);
    }

    @PostMapping
    @ApiOperation(value = "新增")
    public R add(@RequestBody @Valid DeptAddVo vo) throws InterruptedException {
        deptService.add(vo);
        return R.ok();
    }

    @PutMapping
    @ApiOperation(value = "更新")
    public R update(@RequestBody @Valid DeptUpdateVo vo) {
        deptService.updateById(vo);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public R delete(@PathVariable("id") Long id) {
        deptService.delete(id);
        return R.ok();
    }

    @GetMapping("/treeselect")
    @ApiOperation(value = "查询部门下拉树结构")
    public R<List<TreeSelect>> treeselect() {
        return R.ok(deptService.treeselect());
    }

    @GetMapping("/roleDeptTreeselect/{roleId}")
    @ApiOperation(value = "根据角色ID查询部门树结构")
    public R<RoleDeptTreeselectVo> roleDeptTreeselect(@PathVariable("roleId") Long roleId) {
        return R.ok(deptService.roleDeptTreeselect(roleId));
    }

    @GetMapping("/list/exclude/{id}")
    @ApiOperation(value = "查询")
    public R<List<Dept>> listExclude(@PathVariable("id") Long id) {
        List<Dept> page = deptService.listExclude(id);
        return R.ok(page);
    }
}
